package com.seventh.blekit;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//        void OnGUI()
//        {
//        //方法一
//        try{
//        var javaClass=new AndroidJavaObject("com.hello.world.MainActivity");
//        string value1=javaClass.Call<string>("test");
//        GUI.Label(new Rect(10,10,500,500),value1);
//        }catch(System.Exception ex){
//        GUI.Label(new Rect(10,10,500,500),ex.ToString());
//        }
//        //方法二
//        try{
//        AndroidJavaClass jc=new AndroidJavaClass("com.unity3d.player.UnityPlayer");
//        AndroidJavaObject jo=jc.GetStatic<AndroidJavaObject> ("currentActivity");
//        string value2=jo.Call<string> ("test");
//        GUI.Label(new Rect(20,20,500,500),value2);
//        }catch(System.Exception ex){
//        GUI.Label(new Rect(20,20,500,500),ex.ToString());
//        }
//
//        }

/**
 * 定义Unity交互基础方法
 */
public class BaseUnity {
    /**
     * 当前上下文对象
     * PrintWriter
     */
    @NonNull
    protected final Context context;
    //    "receiveObject", "onReceive"
    //调用unity方法需要的
    @NonNull
    private String gameObjectName = "receiveObject";
    //所挂脚本的方法
    @NonNull
    private String functionName = "onReceive";

    public BaseUnity(@NonNull Context context) {
        this.context = context;
    }

    @NonNull
    public Context getContext() {
        return context;
    }

    public void setFunctionName(@NonNull String functionName) {
        this.functionName = functionName;
    }

    @NonNull
    public String getFunctionName() {
        return functionName;
    }

    public void setGameObjectName(@NonNull String gameObjectName) {
        this.gameObjectName = gameObjectName;
    }

    @NonNull
    public String getGameObjectName() {
        return gameObjectName;
    }

    /**
     * Toast显示unity发送过来的内容
     *
     * @param content 消息的内容
     */
    public void showToast(String content) {
        Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
    }


    //    /**
//     * 调用Unity的方法
//     *
//     * @param gameObjectName 调用的GameObject的名称
//     * @param functionName   方法名
//     * @param args           参数
//     * @return 调用是否成功
//     */
//    public boolean callUnity(String gameObjectName, String functionName, String args) {
//        try {
//            Class<?> classType = Class.forName("com.unity3d.player.UnityPlayer");
//            Method method = classType.getMethod("UnitySendMessage", String.class, String.class, String.class);
//            method.invoke(classType, gameObjectName, functionName, args);
//            return true;
//        } catch (ClassNotFoundException ignored) {
//
//        } catch (NoSuchMethodException ignored) {
//
//        } catch (IllegalAccessException ignored) {
//
//        } catch (InvocationTargetException ignored) {
//
//        }
//        return false;
//    }
    //缓存Method和class
    @Nullable
    private static Class<?> UnityPlayer;
    @Nullable
    private static Method UnitySendMessage;

    /**
     * 调用Unity的方法
     *
     * @param gameObjectName 调用的GameObject的名称
     * @param functionName   方法名
     * @param args           参数
     * @return 调用是否成功
     */
    public static boolean callUnity(@NonNull String gameObjectName, @NonNull String functionName, @NonNull String args) {
        try {
            if (UnitySendMessage == null) {
                UnityPlayer = Class.forName("com.unity3d.player.UnityPlayer");
                UnitySendMessage = UnityPlayer.getMethod("UnitySendMessage", String.class, String.class, String.class);
            }
            UnitySendMessage.invoke(UnityPlayer, gameObjectName, functionName, args);
            return true;
        } catch (ClassNotFoundException |
                NoSuchMethodException |
                IllegalAccessException |
                InvocationTargetException ignored) {
            return false;
        }
    }

    /**
     * 获取unity项目的上下文
     *
     * @return _unityActivity
     */
    public static Activity getUnityActivity() {
        try {
            Class<?> classType = Class.forName("com.unity3d.player.UnityPlayer");
            return (Activity) classType.getDeclaredField("currentActivity").get(classType);
        } catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException e) {
            throw new IllegalStateException("无法获取unity context", e);
        }
    }
}
